﻿using EpubFactory.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EpubFactory.Robot.UI
{
    public partial class CoverOptionForm : Form
    {
        public CoverOptionForm()
        {
            InitializeComponent();
        }

        private void lblColor_Click(object sender, EventArgs e)
        {
            colorPicker.ShowDialog();
            ((Label)sender).ForeColor = colorPicker.Color;
        }
        private void lblFont_Click(object sender, EventArgs e)
        {
            fontPicker.ShowDialog();
            ((Label)sender).Font = fontPicker.Font;
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            var option = GetCoverOption();
            pbCover.Image = ImageUtil.Generate("狂人日记", "鲁迅", option);
        }

        private CoverOption GetCoverOption()
        {
            var option = new CoverOption()
            {
                Size = new Size(Int32.Parse(txtWidth.Text), Int32.Parse(txtHeight.Text)),
                Title = new CoverOption.Item()
                {
                    ForeColor = lblTitleForeColor.ForeColor,
                    BgColor = lblTitleBgColor.ForeColor,
                    SimpleFont = new CoverOption.SimpleFont() { Family = lblTitleFont.Font.FontFamily.Name, Size = (int)lblTitleFont.Font.Size },
                },
                Author = new CoverOption.Item()
                {
                    ForeColor = lblAuthorForeColor.ForeColor,
                    BgColor = lblAuthorBgColor.ForeColor,
                    SimpleFont = new CoverOption.SimpleFont() { Family = lblAuthorFont.Font.FontFamily.Name, Size = (int)lblAuthorFont.Font.Size },
                }
            };
            return option;
        }

        private void CoverOptionForm_Load(object sender, EventArgs e)
        {
            var option = AppPreference.Load().CoverOption;
            if (option == null || option.Size.IsEmpty)
            {
                option = AppPreference.DefaultCoverOption;
            }
            txtWidth.Text = option.Size.Width.ToString();
            txtHeight.Text = option.Size.Height.ToString();

            lblAuthorFont.Font = option.Author.SimpleFont.ToFont();
            lblAuthorBgColor.ForeColor = option.Author.BgColor;
            lblAuthorForeColor.ForeColor = option.Author.ForeColor;


            lblTitleBgColor.ForeColor = option.Title.BgColor;
            lblTitleForeColor.ForeColor = option.Title.ForeColor;
            lblTitleFont.Font = option.Title.SimpleFont.ToFont();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var option = GetCoverOption();
            var conf = AppPreference.Load();
            conf.CoverOption = option;
            AppPreference.Save();
        }
    }
}
